package com.wondersgroup.framework.sample.combination.action;

import com.wondersgroup.framework.core.web.struts2.action.AbstractGridAction;
import com.wondersgroup.framework.core5.model.vo.ValueObject;
import com.wondersgroup.framework.sample.combination.vo.FormGridVO;
import com.wondersgroup.framework.sample.combination.vo.HospitalVO;
import com.wondersgroup.framework.sample.component.form.vo.UserVO;
import com.wondersgroup.framework.sample.component.grid.util.GridVOUtil;
import com.wondersgroup.framework.sample.component.grid.util.IdGenerator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;

public class FormGridAction extends AbstractGridAction {
	private FormGridVO formgridVO;

	public FormGridAction() {
		this.formgridVO = new FormGridVO();
	}

	public FormGridVO getGridVO() {
		return this.formgridVO;
	}

	public void setGridVO(FormGridVO gridVO) {
		this.formgridVO = gridVO;
	}

	public ValueObject getValueObject() {
		return this.formgridVO;
	}

	public String save() {
		return null;
	}

	public String update() {
		List hospitalsOfNew = super.getNewRecords(HospitalVO.class);
		List hospitalsOfUpdate = super.getUpdateRecords(HospitalVO.class);
		List hospitalsOfDelete = super.getDeleteRecords(HospitalVO.class);

		List usersOfNew = super.getNewRecords(UserVO.class);
		List usersOfUpdate = super.getUpdateRecords(UserVO.class);
		List usersOfDelete = super.getDeleteRecords(UserVO.class);

		newData(usersOfNew);
		updateData(usersOfUpdate);
		deleteData(usersOfDelete);

		List ids = new ArrayList();
		List personInfoids = new ArrayList();
		for (Iterator i$ = hospitalsOfNew.iterator(); i$.hasNext();) {
			HospitalVO vo = (HospitalVO) i$.next();
			ids.add(vo.getBac909());
		}
		for (Iterator i$ = usersOfNew.iterator(); i$.hasNext();) {
			UserVO vo = (UserVO) i$.next();
			personInfoids.add(vo.getId());
		}

		String result = GridVOUtil.createResult(true,
				new Object[][] { { "personInfoids", personInfoids },
						new Object[] { "ids", ids } });

		this.logger.info("返回数据:" + result);
		createJSonData(result);
		return "ajax";
	}

	public String delete() {
		return null;
	}

	private boolean newData(List<UserVO> data) {
		for (int i = 0; i < data.size(); ++i) {
			UserVO vo = (UserVO) data.get(i);
			vo.setId(String.valueOf(IdGenerator.nextID()));
			this.logger.info("新增记录" + (i + 1) + ":[" + vo.toString() + "]");
		}
		return true;
	}

	private boolean updateData(List<UserVO> data) {
		for (int i = 0; i < data.size(); ++i) {
			UserVO vo = (UserVO) data.get(i);
			this.logger.info("更新记录" + (i + 1) + ":[" + vo.toString() + "]");
		}
		return true;
	}

	private boolean deleteData(List<UserVO> data) {
		for (int i = 0; i < data.size(); ++i) {
			UserVO vo = (UserVO) data.get(i);
			this.logger.info("删除记录" + (i + 1) + ":[" + vo.toString() + "]");
		}
		return true;
	}
}